package cn.edu.djtu.fw.action;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.struts2.interceptor.ServletRequestAware;
import org.springframework.beans.factory.annotation.Autowired;

import cn.edu.djtu.fw.model.Json;
import cn.edu.djtu.fw.model.User;
import cn.edu.djtu.fw.service.UserServiceI;
import cn.edu.djtu.fw.util.JsonUtil;

import com.opensymphony.xwork2.ActionSupport;

public class UserAction extends ActionSupport implements ServletRequestAware {
	private static final long serialVersionUID = 1L;
	private HttpServletRequest request;
	private UserServiceI userService;
	private User user;
	private int page;
	private int rows;
	private String ids;

	@Override
	public String execute() throws Exception {
		Map<String, Object> m = new HashMap<String, Object>();
		m = userService.getAllUser(page, rows, user);
		JsonUtil.writeJson(m);
		return null;
	}

	public String i_login() throws Exception {
		HttpSession session = request.getSession();
		if (!code.equals(session.getAttribute("randomCode"))) {
			session.setAttribute("loginMsg", "* 验证码错误,请重新输入");
			return "index";
		}
		User u = new User();
		u.setUid(uid);
		u.setPwd(pwd);
		User login = new User();
		login = userService.login(u);
		if (login != null) {
			session.setAttribute("user", login);
			return "index";
		} else {
			session.setAttribute("loginMsg", "* 用户名密码错误");
			return "index";
		}
	}

	public String i_loginOut() throws Exception {
		HttpSession session = request.getSession();
		if (session != null && session.getAttribute("user") != null) {
			session.invalidate();
			return "index";
		}
		return "index";
	}

	public String i_reg() throws Exception {
		User u = new User();
		u.setUid(uid);
		u.setRealname(realname);
		u.setPwd(pwd);
		if (userService.insert(u)) {
			return "index";
		}
		return "index";
	}

	public String i_update() throws Exception {
		HttpSession session = request.getSession();
		if (userService.updateSelective(user)) {
			session.setAttribute("u_update_msg", "* 修改成功 重新登录生效！");
			return "user_update";
		}
		session.setAttribute("u_update_msg", "* 修改失败");
		return "user_update";
	}

	public String i_pwd_update() throws Exception {
		HttpSession session = request.getSession();
		User u = new User();
		u.setPwd(newpwd);
		u.setUid(user.getUid());
		if (userService.updateSelective(u)) {
			session.setAttribute("u_pwd_update_msg", "* 修改成功 重新登录生效！");
			return "user_pwd";
		}
		session.setAttribute("u_pwd_update_msg", "* 修改失败");
		return "user_pwd";
	}

	public String add() throws Exception {
		Json j = new Json();
		if (userService.insert(user)) {
			j.setSuccess(true);
			j.setMsg("添加成功！");
		} else {
			j.setMsg("添加失败！");
		}
		JsonUtil.writeJson(j);
		return null;
	}

	public String update() throws Exception {
		Json j = new Json();
		if (userService.update(user)) {
			j.setSuccess(true);
			j.setMsg("修改成功！");
		} else {
			j.setMsg("修改失败！");
		}
		JsonUtil.writeJson(j);
		return null;
	}

	public String delete() throws Exception {
		Json j = new Json();
		String[] idsStr = ids.split(",");
		for (int i = 0; i < idsStr.length; i++) {
			User u = userService.getUserById(Integer.parseInt(idsStr[i]));
			if (u != null) {
				userService.delete(u.getUid());
			}
		}
		j.setSuccess(true);
		j.setMsg("删除成功！");
		JsonUtil.writeJson(j);
		return null;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public UserServiceI getUserService() {
		return userService;
	}

	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	@Override
	public void setServletRequest(HttpServletRequest req) {
		this.request = req;
	}

	private Integer uid;

	private String pwd;

	private String realname;

	private String phone;

	private String email;

	private String address;
	private String code;

	public HttpServletRequest getRequest() {
		return request;
	}

	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public Integer getUid() {
		return uid;
	}

	public void setUid(Integer uid) {
		this.uid = uid;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

	public String getRealname() {
		return realname;
	}

	public void setRealname(String realname) {
		this.realname = realname;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	private String oldpwd;
	private String newpwd;

	public String getOldpwd() {
		return oldpwd;
	}

	public void setOldpwd(String oldpwd) {
		this.oldpwd = oldpwd;
	}

	public String getNewpwd() {
		return newpwd;
	}

	public void setNewpwd(String newpwd) {
		this.newpwd = newpwd;
	}

}
